//
//  LoginViewController.m
//  OrangeNote
//
//  Created by Vandals on 16/1/22.
//  Copyright © 2016年 崇龙. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginButton.h"
#import "StTransitions.h"
#import "MainViewController.h"
#import "RegisterViewController.h"
#import "UserDataManager.h"

#define kWidhth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height


@interface LoginViewController ()<UIViewControllerTransitioningDelegate,UITextFieldDelegate>
@property(nonatomic,strong)UIScrollView *scrollView;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor orangeColor];
    self.navigationItem.titleView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"biaotilan"]];

    [self addSubViews];
    
}
-(void)addSubViews{
//    // 标题
//    UIView *navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidhth, 64*kWidhth/320)];
//    navView.backgroundColor = [UIColor blackColor];
//    [self.view addSubview:navView];
//    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20*kWidhth/320, kWidhth, 44*kWidhth/320)];
//    titleLabel.backgroundColor = [UIColor blackColor];
//    titleLabel.textAlignment = 1;
//    titleLabel.text = @"登陆页面";
//    titleLabel.textColor = [UIColor whiteColor];
//    titleLabel.font = [UIFont systemFontOfSize:18];
//    [navView addSubview:titleLabel];
    
    
    //scroll view
    
    UIImageView *imv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"333.jpg"]];
    imv.frame = CGRectMake(0, 0, kWidhth, kHeight);
    
//    UIImageView *imvv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"20.jpg"]];
//    imvv.frame = CGRectMake(0, kHeight, kWidhth, kHeight);
    UIImageView *imv1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"333.jpg"]];
    imv1.frame = CGRectMake(0, kHeight, kWidhth, kHeight);
    UIImageView *imv2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"333.jpg"]];
    imv2.frame = CGRectMake(0, kHeight*2, kWidhth, kHeight);
    UIImageView *imv3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"333.jpg"]];
    imv3.frame = CGRectMake(0, kHeight*3, kWidhth, kHeight);
    UIImageView *imv4 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"333.jpg"]];
    imv4.frame = CGRectMake(0, kHeight*4,kWidhth, kHeight);
    UIImageView *imv5 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"333.jpg"]];
    imv5.frame = CGRectMake(0, kHeight*5, kWidhth, kHeight);
    UIImageView *imv6 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"333.jpg"]];
    imv6.frame = CGRectMake(0, kHeight*6, kWidhth, kHeight);
    self.scrollView = [[UIScrollView alloc]init];
    self.scrollView.frame = CGRectMake(0, 0, kWidhth, kHeight);
    self.scrollView.contentSize = CGSizeMake(kWidhth, kHeight*7);
//    self.scrollView.alwaysBounceVertical = YES;//垂直
        self.scrollView.scrollEnabled = NO;
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:imv];
    [self.scrollView addSubview:imv1];
    [self.scrollView addSubview:imv2];
    [self.scrollView addSubview:imv3];
    [self.scrollView addSubview:imv4];
    [self.scrollView addSubview:imv5];
    [self.scrollView addSubview:imv6];
    
    
    [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];

    
    
    // 用户名label
    _userTF = [[UITextField alloc]initWithFrame:CGRectMake(5, kHeight/2 - 200, kWidhth-10, 60)];
    _userTF.delegate = self;
    _userTF.placeholder = @"请输入用户名";
    [_userTF setValue:[UIColor orangeColor] forKeyPath:@"_placeholderLabel.textColor"];
    [_userTF setValue:[UIFont boldSystemFontOfSize:20] forKeyPath:@"_placeholderLabel.font"];
    _userTF.textAlignment = NSTextAlignmentCenter;
    _userTF.backgroundColor = [UIColor lightTextColor];
    _userTF.textColor = [UIColor redColor];
    _userTF.layer.cornerRadius = 10;
    _userTF.layer.masksToBounds = YES;
    _userTF.layer.borderColor = [[UIColor orangeColor]CGColor];
    _userTF.layer.borderWidth = 1.0;
    _userTF.alpha = 0.8;
    _userTF.font = [UIFont systemFontOfSize:24];
    [self.view addSubview:_userTF];
    
    // 密码
    _passWordTF = [[UITextField alloc]initWithFrame:CGRectMake(5, kHeight/2 - 100, kWidhth - 10, 60)];
    _passWordTF.delegate = self;
    _passWordTF.placeholder = @"请输入密码";
    _passWordTF.textColor = [UIColor redColor];
    [_passWordTF setValue:[UIColor orangeColor] forKeyPath:@"_placeholderLabel.textColor"];
    [_passWordTF setValue:[UIFont boldSystemFontOfSize:20] forKeyPath:@"_placeholderLabel.font"];
    _passWordTF.textAlignment = NSTextAlignmentCenter;
    _passWordTF.backgroundColor = [UIColor lightTextColor];
    _passWordTF.layer.masksToBounds = YES;
    _passWordTF.layer.cornerRadius = 10;
    _passWordTF.layer.borderWidth = 1.0;
    _passWordTF.layer.borderColor = [[UIColor orangeColor]CGColor];
    _passWordTF.alpha = 0.8;
    _passWordTF.secureTextEntry = YES;
    _passWordTF.font = [UIFont systemFontOfSize:24];
    [self.view addSubview:_passWordTF];
    
    // 登陆按钮
    LoginButton *logBTN = [[LoginButton alloc]initWithFrame:CGRectMake(kWidhth/2 - 150, kHeight/2, 300, 60)];
    logBTN.backgroundColor = [UIColor orangeColor];
    logBTN.alpha = 0.7;
    [self.view addSubview:logBTN];
    
    [logBTN setTitle:@"登陆" forState:UIControlStateNormal];
    [logBTN addTarget:self action:@selector(PresentCtrl:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    // 注册按钮
    UIButton *regBTN = [[UIButton alloc]initWithFrame:CGRectMake(kWidhth/2 - 150, kHeight/2 + 80,300, 60)];
    regBTN.backgroundColor = [UIColor orangeColor];
    regBTN.layer.cornerRadius = 30;
    regBTN.alpha = 0.7;
    [regBTN setTitle:@"注册" forState:UIControlStateNormal];
    [regBTN addTarget:self action:@selector(registerLogin:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:regBTN];
    
    
    
    
}

// timer轮播方法
CGFloat myNumm = 0;
-(void)scrollTimer{
    myNumm += 0.5;
    _scrollView.contentOffset = CGPointMake(0, myNumm);
    if (myNumm == kHeight*7) {
        _scrollView.contentOffset = CGPointMake(0, 0);
        myNumm = 0;
    }
    
}

-(void)registerLogin:(LoginButton *)regist{
    RegisterViewController *RegVC = [[RegisterViewController alloc]init];
    [self.navigationController pushViewController:RegVC animated:YES];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_userTF resignFirstResponder];
    [_passWordTF resignFirstResponder];
    
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [_userTF resignFirstResponder];
    [_passWordTF resignFirstResponder];
    return YES;
}

// 模拟网络访问
-(void)PresentCtrl:(LoginButton *)loginBtn{
    typeof(self) __weak weak = self;
    // 模拟网络访问
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC)),  dispatch_get_main_queue(), ^{
        [weak loginBtnAction:loginBtn];
    });
}

// 判断登陆
-(void)loginBtnAction:(LoginButton *)loginBtn{
    typeof(self) __weak weak = self;
    if (_userTF.text == nil || _userTF.text == NULL || [_userTF.text isEqualToString:@""]) {
        // 按钮动画还原
        [loginBtn ErrorRevertAnimationCompletion:nil];
        // 延迟
        [self performSelector:@selector(userAlertAction) withObject:nil afterDelay:1.0];
        
    }else if (_passWordTF.text == nil || _passWordTF.text == NULL || [_passWordTF.text isEqualToString:@""]){
        // 按钮动画还原
        [loginBtn ErrorRevertAnimationCompletion:nil];
        // 延迟调用弹出提示框方法
        [self performSelector:@selector(passWordAlertAction) withObject:nil afterDelay:1.0];
    }else{
        // 用户名和密码输入正确
        
        UserDataManager *manager = [[UserDataManager alloc]init];
        [manager userLoginWithUsername:_userTF.text andPassword:_passWordTF.text];
        manager.loginSuccessBlock = ^(){
            NSLog(@"登陆成功,执行跳转!");
            [loginBtn ExitAnimationCompletion:^{
                [weak didPresentControllerButtonTouch];
            }];
        };
        
        manager.loginFailedBlock = ^(NSString *message){
            
            
            [self loginErrorAlert:message];
            
            
            [loginBtn ErrorRevertAnimationCompletion:nil];
        };

    }
}


- (void)loginErrorAlert:(NSString *)message{
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *btnAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:nil];
    [alertC addAction:btnAction];
    [self presentViewController:alertC animated:YES completion:nil];
    
}


// 弹出输入用户提示框方法
-(void)userAlertAction{
    UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名不能为空" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *btnAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertCtrl addAction:btnAction];
    [self presentViewController:alertCtrl animated:YES completion:nil];
}
// 弹出输入密码提示框方法
-(void)passWordAlertAction{
    UIAlertController *alertCtrl = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *btnAction = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertCtrl addAction:btnAction];
    [self presentViewController:alertCtrl animated:YES completion:nil];
}



// 登陆成功跳转方法
-(void)didPresentControllerButtonTouch{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                  presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    
    return [[StTransitions alloc]initWithTransitionDuration:0.4f StartingAlpha:0.5f isBOOL:true];
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    
    return [[StTransitions alloc]initWithTransitionDuration:0.4f StartingAlpha:0.8f isBOOL:false];
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
